<?php

class widget_ui_posts extends WP_Widget {
	function widget_ui_posts() {
		$widget_ops = array( 'classname' => 'widget_ui_posts', 'description' => '推荐文章显示' );
		$this->WP_Widget( 'widget_ui_posts', 'L-推荐内容', $widget_ops );
	}

	function widget( $args, $instance ) {
		extract( $args );

		$title        = apply_filters('widget_name', $instance['title']);
		$limit        = $instance['limit'];
		$cat          = $instance['cat'];
		$orderby      = $instance['orderby'];
        $subtitle     = $instance['subtitle'];

		$style=' class="list-unstyled clearfix" ';
        //		echo $before_widget;
        //		echo $before_title.$title.$after_title;
        echo '<div class="col-xs-12 col-sm-6 col-md-12 col-lg-12">';
        echo '<h4 class="ny-h4">' . $title . ' <small>' . $subtitle .'</small></h4>';
		echo '<ul'.$style.'>';
		echo dtheme_posts_list( $orderby,$limit,$cat);
		echo '</ul>';
        echo '</div>';
		//echo $after_widget;
	}

	function form( $instance ) {
		$defaults = array( 
			'title' => '热门文章', 
			'limit' => 6, 
			'orderby' => 'comment_count',
            'subtitle' => 'Recommended',
		);
		$instance = wp_parse_args( (array) $instance, $defaults );
?>
		<p>
			<label>
				标题：
				<input style="width:100%;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" />
			</label>
		</p>
                <p>
			<label>
				副标题：
				<input style="width:100%;" id="<?php echo $this->get_field_id('subtitle'); ?>" name="<?php echo $this->get_field_name('subtitle'); ?>" type="text" value="<?php echo $instance['subtitle']; ?>" />
			</label>
		</p>
		<p>
			<label>
				排序：
				<select style="width:100%;" id="<?php echo $this->get_field_id('orderby'); ?>" name="<?php echo $this->get_field_name('orderby'); ?>" style="width:100%;">
					<option value="comment_count" <?php selected('comment_count', $instance['orderby']); ?>>评论数</option>
					<option value="date" <?php selected('date', $instance['orderby']); ?>>发布时间</option>
					<option value="rand" <?php selected('rand', $instance['orderby']); ?>>随机</option>
				</select>
			</label>
		</p>
		<p>
			<label>
				分类限制：
				<a style="font-weight:bold;color:#f60;text-decoration:none;" href="javascript:;" title="格式：1,2 &nbsp;表限制ID为1,2分类的文章&#13;格式：-1,-2 &nbsp;表排除分类ID为1,2的文章&#13;也可直接写1或者-1；注意逗号须是英文的">？</a>
				<input style="width:100%;" id="<?php echo $this->get_field_id('cat'); ?>" name="<?php echo $this->get_field_name('cat'); ?>" type="text" value="<?php echo $instance['cat']; ?>" size="24" />
			</label>
		</p>
		<p>
			<label>
				显示数目：
				<input style="width:100%;" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="number" value="<?php echo $instance['limit']; ?>" size="24" />
			</label>
		</p>		
	<?php
	}
}


function dtheme_posts_list($orderby,$limit,$cat) {
	$args = array(
		'order'            => 'DESC',
		'cat'              => $cat,
		'orderby'          => $orderby,
		'showposts'        => $limit,
		'category__not_in' => array(211),
		'ignore_sticky_posts' => 1
	);
	query_posts($args);
	while (have_posts()) : the_post(); 
?>
     <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a><small class="pull-right"><?php the_time('m-d');?></small></li>
<?php
    endwhile; wp_reset_query();
}